    //
//  MonthViewController.m
//  MonthView
//
//  Created by  ibokan on 10-8-23.
//  Copyright 2010 tencent.com. All rights reserved.
//

#import "CalendarViewController.h"
#import "MonthView.h"
#import "NSDate-Helper.h"
#import <QuartzCore/QuartzCore.h>
#import <CoreGraphics/CoreGraphics.h>

@implementation CalendarViewController
@synthesize selectedDate;

@synthesize _scrollView;

-(void) back
{
	
}

-(void) goBack
{
}

-(id) init 
{
	if(self = [super init])
	{
		UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 7, 100, 30)];
		titleLable.text = @" 活动日历";
		titleLable.backgroundColor = [UIColor clearColor];
		titleLable.font = [UIFont systemFontOfSize:20];
		self.navigationItem.titleView = titleLable;
		[titleLable release];
		
	
		UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
		button1.backgroundColor = [UIColor clearColor];
		[button1 addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
		UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button1];
		self.navigationItem.leftBarButtonItem = backButton;
		[backButton release];
	}
	
	return self;
}

-(void) loadView
{
	selectedDate = [NSDate date];
	
	self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, kCalendarHeight)];
	self.view.backgroundColor = [UIColor clearColor];
	self.view.layer.contents = (id)[UIImage imageNamed:@"dayTableViewBG.png"].CGImage;
	
	UIImageView *tipImageView = [[UIImageView alloc] initWithFrame:CGRectMake(55, 321, 6, 6)];
	tipImageView.image = [UIImage imageNamed:@"cal_point0.png"];
	tipImageView.backgroundColor = [UIColor clearColor];
	[self.view addSubview:tipImageView];
	[tipImageView release];
	
	UIImageView *tipImageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(175, 321, 6, 6)];
	tipImageView1.image = [UIImage imageNamed:@"cal_point1.png"];
	tipImageView1.backgroundColor = [UIColor clearColor];
	[self.view addSubview:tipImageView1];
	[tipImageView1 release];
	
	UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(155, 315, 2, 18)];
	[line setImage:[UIImage imageNamed:@"line.png"]];
	[self.view addSubview:line];
	[line release];
	
	
	UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 312, 200, 25)];
	[tipLabel setBackgroundColor:[UIColor clearColor]];
	[tipLabel setFont:[UIFont systemFontOfSize:12]];
	[tipLabel setText:@"都市新主张                    艺术地图"];
	[tipLabel setTextColor:[UIColor grayColor]];
	[self.view addSubview:tipLabel];
	[tipLabel release];
	
	leftMonthView = [MonthView monthViewWithDate:[selectedDate  dateafterMonth:-1] Frame:CGRectMake(0, 0, 320, kCalendarHeight)];
    
	midMonthView = [MonthView monthViewWithDate:selectedDate  Frame:CGRectMake(320, 0, 320, kCalendarHeight)];
    
	rightMonthView = [MonthView monthViewWithDate:[selectedDate dateafterMonth:1] Frame:CGRectMake(640,0, 320, kCalendarHeight)];
	
	_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, kCalendarHeight)];
	[_scrollView setBackgroundColor:[UIColor clearColor]];
    
	//不显示水平和竖直拉动条
	_scrollView.showsHorizontalScrollIndicator = NO;
	_scrollView.showsVerticalScrollIndicator = NO;
	_scrollView.contentSize = CGSizeMake(320 * 3, kCalendarHeight);
    
	//每次移动与0点的相对位移量
	_scrollView.contentOffset = CGPointMake(320, 0);
	_scrollView.delegate = self;
	_scrollView.pagingEnabled = YES;
	_scrollView.scrollEnabled = YES;
	[self._scrollView addSubview:leftMonthView];
	[self._scrollView addSubview:midMonthView];
	[self._scrollView addSubview:rightMonthView];
	
	[self.view addSubview:_scrollView];
}



//手指滑动时切换视图
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
	//此时应是左视图移到中间了,应将右视图remove 再将左视图加上,这样一直保持着左中右的视图存在
	if(_scrollView.contentOffset.x < 320)
	{
		[rightMonthView removeFromSuperview];
		rightMonthView = midMonthView;
		rightMonthView.frame = CGRectMake(640, 0, 320, kCalendarHeight);
		midMonthView = leftMonthView;
		midMonthView.frame = CGRectMake(320, 0, 320, kCalendarHeight);
		leftMonthView = [MonthView monthViewWithDate:[midMonthView.currentDate dateafterMonth:-1] Frame:CGRectMake(0, 0, 320, kCalendarHeight)];
		[self._scrollView addSubview:leftMonthView];
		self._scrollView.contentOffset = CGPointMake(320, 0);
		
	}
	//此时应是右视图居中,contentOffset.x = 640了;
	if(_scrollView.contentOffset.x > 320)
	{
		[leftMonthView removeFromSuperview];
		leftMonthView = midMonthView;
		leftMonthView.frame = CGRectMake(0, 0, 320, kCalendarHeight);
		midMonthView = rightMonthView;
		midMonthView.frame = CGRectMake(320, 0, 320, kCalendarHeight);
		rightMonthView = [MonthView  monthViewWithDate:[midMonthView.currentDate dateafterMonth:1] Frame:CGRectMake(640, 0, 320, kCalendarHeight)];
		[self._scrollView addSubview:rightMonthView];
		self._scrollView.contentOffset = CGPointMake(320, 0);
	}
}


//点击button时切换视图
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
	//此时应是左视图移到中间了,应将右视图remove 再将左视图加上,这样一直保持着左中右的视图存在
	if(_scrollView.contentOffset.x < 320)
	{
		[rightMonthView removeFromSuperview];
		rightMonthView = midMonthView;
		rightMonthView.frame = CGRectMake(640, 0, 320, kCalendarHeight);
		midMonthView = leftMonthView;
		midMonthView.frame = CGRectMake(320, 0, 320, kCalendarHeight);
		leftMonthView = [MonthView monthViewWithDate:[midMonthView.currentDate dateafterMonth:-1] Frame:CGRectMake(0, 0, 320, kCalendarHeight)];
		[self._scrollView addSubview:leftMonthView];
		self._scrollView.contentOffset = CGPointMake(320, 0);
		
	}
	//此时应是右视图居中,contentOffset.x = 640了;
	if(_scrollView.contentOffset.x > 320)
	{
		[leftMonthView removeFromSuperview];
		leftMonthView = midMonthView;
		leftMonthView.frame = CGRectMake(0, 0, 320, kCalendarHeight);
		midMonthView = rightMonthView;
		midMonthView.frame = CGRectMake(320, 0, 320, kCalendarHeight);
		rightMonthView = [MonthView  monthViewWithDate:[midMonthView.currentDate dateafterMonth:1] Frame:CGRectMake(640, 0, 320, kCalendarHeight)];
		[self._scrollView addSubview:rightMonthView];
		self._scrollView.contentOffset = CGPointMake(320, 0);
	}
}



- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc 
{
	[_scrollView release];
    [super dealloc];
}

@end





